import { createRouter, createWebHistory } from 'vue-router'
import LoginView from "../views/LoginView.vue";
import RegisterView from "../views/RegisterView.vue";
import HomeView from "../views/HomeView.vue";
import IndexView from "../views/Home/IndexView.vue";
import TestView from '../views/TestView.vue';
import {useUserStore} from "../stores/user.store.js";
const router = createRouter({
    history:createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/login',
            name: 'login',
            component: LoginView,
        },
        {
            path: '/register',
            name: 'register',
            component: RegisterView,
        },
        {
            path: '/test',
            name: 'test',
            component: TestView,
            children:[
                {
                    path:'',
                    name:'test-index',
                    component:()=>import('../views/Test/TestIndexView.vue'),
                },
                {
                    path:'dialog',
                    name:'dialog',
                    component:()=>import('../views/Test/TestDialogView.vue'),
                },
                {
                    path:'api',
                    name:'api',
                    component:()=>import('../views/Test/TestApiView.vue'),
                },
            ]
        },
        {
            path:'/',
            name:'home',
            component:HomeView,
            children:[
                {
                    path:'/',
                    name:'index',
                    component:IndexView
                },
                {
                    path:'/users',
                    name:'users',
                    component:()=>import('../views/Home/UsersView.vue'),
                },
                {
                    path:'/todos',
                    name:'todos',
                    component:()=>import('../views/Home/TodosView.vue'),
                },
                {
                    path:'/todos/detail/:id',
                    name:'todos-detail',
                    component:()=>import('../views/Home/TodoDetail.vue'),
                },
                {
                    path:'/account',
                    name:'account',
                    component:()=>import('../views/Home/AccountView.vue'),
                    children:[
                        {
                            path:'index',
                            name:'account-index',
                            component:()=>import('../views/Home/Account/IndexView.vue'),
                        },
                        {
                            path:'setting',
                            name:'account-setting',
                            component:()=>import('../views/Home/Account/SettingView.vue'),
                        },
                        {
                            name:'account-messages',
                            path:'messages',
                            component:()=>import('../views/Home/Account/MessagesView.vue'),
                        },
                        {
                            name:'account-todos',
                            path:'todos',
                            component:()=>import('../views/Home/Account/TodosView.vue'),
                        }
                    ]
                }
            ]
        }
    ]
});
router.beforeEach(async (to, from) => {
    const userStore = useUserStore()
    const isAuthenticated = userStore.isLogin
    if (
        // 检查用户是否已登录
        !isAuthenticated &&
        // ❗️ 避免无限重定向
        to.name !== 'login' && to.fullPath.startsWith('/account')
    ) {
        // 将用户重定向到登录页面
        return { name: 'login' }
    }
    // if(isAuthenticated && to.name === 'login'){
    //   return { name: 'index' }
    // }
})

export default router
